package com.haojiang.mh.ui.adapter

import android.content.Context
import android.graphics.Rect
import android.view.View

import androidx.recyclerview.widget.RecyclerView

import com.haojiang.mh.utils.SizeUtil

/**
 * 自定义Item间隔
 */
class ItemSpaceDecoration(ctx: Context, startAndEnd: Int, top: Int, bottom: Int, extraTop: Int) :
    RecyclerView.ItemDecoration() {

    private val startAndEnd = SizeUtil.dp2px(ctx, startAndEnd.toFloat())
    private val top = SizeUtil.dp2px(ctx, top.toFloat())
    private val bottom = SizeUtil.dp2px(ctx, bottom.toFloat())
    private val extraTop = SizeUtil.dp2px(ctx, extraTop.toFloat())

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        val first = parent.getChildAt(0)
        outRect.top = if (first != null && first == view) top + extraTop else top
        outRect.bottom = bottom
        outRect.left = startAndEnd
        outRect.right = startAndEnd
    }

}
